import 'dart:async';
import 'dart:io';
import 'package:flutter/services.dart';
import 'models/player_state.dart';
import 'models/media_info.dart';
import 'models/player_config.dart';
import 'ffmpeg_kit_engine.dart';

class FigoPlayerEngine {
  static const MethodChannel _channel = MethodChannel('figo_player_plugin/engine');
  final FFmpegKitEngine _ffmpegEngine;
  
  // 流控制器
  final StreamController<PlayerState> _stateController = StreamController.broadcast();
  final StreamController<double> _timeController = StreamController.broadcast();
  final StreamController<MediaInfo> _mediaInfoController = StreamController.broadcast();
  final StreamController<String> _errorController = StreamController.broadcast();
  
  // 当前状态
  PlayerState _currentState = PlayerState.idle;
  double _currentTime = 0.0;
  double _duration = 0.0;
  double _volume = 1.0;
  MediaInfo? _mediaInfo;
  
  FigoPlayerEngine() : _ffmpegEngine = FFmpegKitEngine() {
    _channel.setMethodCallHandler(_handleMethodCall);
    _setupFFmpegEngineListeners();
  }
  
  void _setupFFmpegEngineListeners() {
    _ffmpegEngine.stateStream.listen((state) {
      _currentState = state;
      _stateController.add(state);
    });
    
    _ffmpegEngine.timeStream.listen((time) {
      _currentTime = time;
      _timeController.add(time);
    });
    
    _ffmpegEngine.mediaInfoStream.listen((info) {
      _mediaInfo = info;
      _mediaInfoController.add(info);
    });
    
    _ffmpegEngine.errorStream.listen((error) {
      _errorController.add(error);
    });
  }
  
  Future<void> _handleMethodCall(MethodCall call) async {
    switch (call.method) {
      case 'onStateChanged':
        _currentState = PlayerState.values[call.arguments];
        _stateController.add(_currentState);
        break;
      case 'onTimeUpdate':
        _currentTime = call.arguments;
        _timeController.add(_currentTime);
        break;
      case 'onMediaInfo':
        _mediaInfo = MediaInfo.fromMap(Map<String, dynamic>.from(call.arguments));
        _mediaInfoController.add(_mediaInfo!);
        break;
      case 'onError':
        _errorController.add(call.arguments);
        break;
    }
  }
  
  // 基本控制方法
  Future<bool> loadMedia(String filePath, {PlayerConfig? config}) async {
    return await _ffmpegEngine.loadMedia(filePath, config: config);
  }
  
  Future<bool> play() async {
    return await _ffmpegEngine.play();
  }
  
  Future<bool> pause() async {
    return await _ffmpegEngine.pause();
  }
  
  Future<bool> stop() async {
    return await _ffmpegEngine.stop();
  }
  
  Future<bool> seek(double timeSeconds) async {
    return await _ffmpegEngine.seek(timeSeconds);
  }
  
  // 音量控制
  Future<void> setVolume(double volume) async {
    await _ffmpegEngine.setVolume(volume);
    _volume = volume;
  }
  
  // 移动端优化
  Future<void> setHardwareDecode(bool enable) async {
    await _ffmpegEngine.setHardwareDecode(enable);
  }
  
  Future<void> setBatteryOptimization(bool enable) async {
    await _ffmpegEngine.setBatteryOptimization(enable);
  }
  
  Future<void> setMemoryLimit(int mbLimit) async {
    await _ffmpegEngine.setMemoryLimit(mbLimit);
  }
  
  // 状态查询
  PlayerState get currentState => _currentState;
  double get currentTime => _currentTime;
  double get duration => _duration;
  double get volume => _volume;
  MediaInfo? get mediaInfo => _mediaInfo;
  
  // 流监听
  Stream<PlayerState> get stateStream => _stateController.stream;
  Stream<double> get timeStream => _timeController.stream;
  Stream<MediaInfo> get mediaInfoStream => _mediaInfoController.stream;
  Stream<String> get errorStream => _errorController.stream;
  
  void dispose() {
    _ffmpegEngine.dispose();
    _stateController.close();
    _timeController.close();
    _mediaInfoController.close();
    _errorController.close();
  }
}
